<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use app\common\library\Auth;

/**
 * 师傅管理
 *
 * @icon fa fa-circle-o
 */
class Master extends Backend
{

    /**
     * Master模型对象
     * @var \app\admin\model\Master
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Master;
        $this->view->assign("statusList", $this->model->getStatusList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
//                ->with('group')
                ->where(['group_id'=>2])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
                $province = \app\admin\model\Area::where(['id'=>$v->province_id])->value('name');
                $city = \app\admin\model\Area::where(['id'=>$v->city_id])->value('name');
                $area = \app\admin\model\Area::where(['id'=>$v->area_id])->value('name');

                $v['region'] = $province.'/'.$city.'/'.$area;
            }
            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        return parent::add();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        if ($this->request->isPost()) {
            $this->token();
        }
        $row = $this->model->get($ids);
        $isexist = $this->model->where(['mobile'=>$row['mobile'],'group_id'=>$row['group_id']])->find();
        if($isexist && $isexist['id'] != $ids){
            $this->error('该手机号已存在');
        }

        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $this->view->assign('groupList', build_select('row[group_id]', \app\admin\model\UserGroup::column('id,name'), $row['group_id'], ['class' => 'form-control selectpicker']));
        return parent::edit($ids);
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if (!$this->request->isPost()) {
            $this->error(__("Invalid parameters"));
        }
        $ids = $ids ? $ids : $this->request->post("ids");
        $row = $this->model->get($ids);
        $this->modelValidate = true;
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        Auth::instance()->delete($row['id']);
        $this->success();
    }

    // 指派列表
    public function allocated ()
    {
        $ids = $this->request->param('ids', 0);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model
//                ->with('group')
                ->where(['group_id'=>2])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);
            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
                $province = \app\admin\model\Area::where(['id'=>$v->province_id])->value('name');
                $city = \app\admin\model\Area::where(['id'=>$v->city_id])->value('name');
                $area = \app\admin\model\Area::where(['id'=>$v->area_id])->value('name');

                $v['region'] = $province.'/'.$city.'/'.$area;
            }
            $result = array("total" => $list->total(), "rows" => $list->items(),'ids'=>$ids);

            return json($ids);
        }
        $this->assignconfig('ids', $ids);//加在实例化前
        return $this->view->fetch();
    }
    
    // 附近列表
    public function nearby ()
    {
        $address_lng = $this->request->get('address_lng', 0);
        $address_lat = $this->request->get('address_lat', 0);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // 获取列表列表
            $distance = 30;
            $latitude = $address_lat;
            $longitude = $address_lng;

            //1.计算最大最小经纬度范围
            $range  = 180 / pi() * $distance / 6378.138; //搜索 N km 之内
            $lngR   = $range / cos($latitude * pi() / 180);
            $maxLat = $latitude + $range; //最大纬度
            $minLat = $latitude - $range; //最小纬度
            $maxLng = $longitude + $lngR; //最大经度
            $minLng = $longitude - $lngR; //最小经度

//            return [$minLat,$maxLat];
            $list = $this->model
//                ->with('group')
                ->where(['group_id'=>2])
                ->where(['lat'=>['between',[$minLat,$maxLat]]])
                ->where(['lng'=>['between',[$minLng,$maxLng]]])
                ->where($where)
                ->order($sort, $order)
                ->paginate($limit);

            foreach ($list as $k => $v) {
                $v->avatar = $v->avatar ? cdnurl($v->avatar, true) : letter_avatar($v->nickname);
                $v->hidden(['password', 'salt']);
                $province = \app\admin\model\Area::where(['id'=>$v->province_id])->value('name');
                $city = \app\admin\model\Area::where(['id'=>$v->city_id])->value('name');
                $area = \app\admin\model\Area::where(['id'=>$v->area_id])->value('name');

                $v['region'] = $province.'/'.$city.'/'.$area;
            }
            $result = array("total" => $list->total(), "rows" => $list->items());
            return json($result);
        }
        return $this->view->fetch();
    }
}
